﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace BulkSMSapplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBoxUsername_TextChanged(object sender, EventArgs e)
        {
            
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            //Opening SQL connection
            String connectionString = BulkSMSapplication.Properties.Settings.Default.ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            try
            {
                //
                String checkUser = "SELECT COUNT(*) from [User] WHERE Email = '" + TextBoxUsername.Text + "';";
                SqlCommand cmd = new SqlCommand(checkUser, conn);
                int temp = int.Parse(cmd.ExecuteScalar().ToString());
                conn.Close();

                if (temp >= 1)
                {
                    conn.Open();
                    string checkPasswordQuery = "SELECT Password FROM [User] WHERE Email = '" + TextBoxUsername.Text + "';";
                    SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
                    string password = passComm.ExecuteScalar().ToString();
                    if(password == TextBoxPassword.Text)
                    {
                        Session["UserName"] = TextBoxUsername.Text;
                        Response.Write("<script language='javascript'>alert('Password correct.');</script>");
                        Response.Redirect("../MenuItems/SendSMS.aspx");
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('Password incorrect.');</script>");
                    }
                }
                else
                {
                    //
                    Response.Write("<script language='javascript'>alert('No user with that email exists.');</script>");

                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.ToString());
                Response.Write("<script language='javascript'>alert('Doesn't work.');</script>");
            }
            conn.Close();
        }
    }
}